/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package khmhappointmentsystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;

/**
 *
 * @author mbelisle
 */
public class Environment
{

    private static Connection dbConn;
    //ResourceMap resourceMap = getResourceMap();
    static String applicationName = ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("Application.name");
    //Create the Database Object
    static String dbDriver = ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("DBDriver");
    static String connectionString = ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("DBConnection");
    private static JDesktopPane desktopPane = null;

    /**
     * @return the dbConn
     */
    public static Connection getDbConn()
    {
        return dbConn;
    }

    public static void createConnection()
    {
        boolean connectSucessful = true;
        //Try to connect to the database
        try
        {
            System.out.println("About to connect to the database.");
            //Create database connection objects.
            Class.forName(dbDriver);
            dbConn = DriverManager.getConnection(connectionString, "java_user", "khmh");
            System.out.println("Successfully connected to the database.");
        }
        catch (ClassNotFoundException cnfEx)
        {
            connectSucessful = false;
            System.out.println("ERROR: The driver specified could not be found.");
            cnfEx.printStackTrace();
        }
        catch (SQLException sqlEx)
        {
            connectSucessful = false;
            System.out.println("ERROR: Could not connect to the database.");
            sqlEx.printStackTrace();
        }
        catch (Exception e)
        {
            connectSucessful = false;
            System.out.println("ERROR: Could not connect to the database.\nSome other error occured.");
            e.printStackTrace();
        }

        //Test if we were able to connect to the database.
        if (connectSucessful == true)
        {
        }
        else
        {
            JOptionPane.showMessageDialog(null, "An error occured.\nCould not connect to the database.", applicationName, JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }

    //Populate the table with data from the Clinic table
    public static Vector getClinic() throws Exception
    {

        Vector<Vector<String>> clinicVector = new Vector<Vector<String>>();

        PreparedStatement prep = dbConn.prepareStatement("select * from clinic");

        ResultSet rs = prep.executeQuery();
        while (rs.next())
        {
            Vector<String> clinic = new Vector<String>();
            clinic.add(rs.getString(2));
            clinic.add(rs.getString(3));
            clinic.add(rs.getString(4));
            clinicVector.add(clinic);

        }
        return clinicVector;
    }

    /**
     * @return the desktopPane
     */
    public static JDesktopPane getDesktopPane()
    {
        return desktopPane;
    }

    /**
     * @param aDesktopPane the desktopPane to set
     */
    public static void setDesktopPane(JDesktopPane aDesktopPane)
    {
        desktopPane = aDesktopPane;
    }
}
